﻿using System;
using System.Web.UI;

namespace CMS.Common {
	/// <summary>
	/// 一些常用的Js调用
	/// </summary>
	public class JScript {
		/// <summary>
		/// 弹出 javascript 消息窗口
		/// </summary>
		/// <param name="message">弹出消息内容</param>
		public static void Alert(Page page, string message) {
			string js = string.Format(@"<script language='javascript'>alert('{0}');</script>", message);

			if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "Alert"))
				page.ClientScript.RegisterStartupScript(page.GetType(), "Alert", js);
		}

		/// <summary>
		/// 弹出消息框并且转向到新的URL
		/// </summary>
		/// <param name="message">弹出消息内容</param>
		/// <param name="toURL">跳转地址</param>
		public static void AlertAndRedirect(Page page, string message, string url) {
			string js = string.Format(@"<script language='javascript'>alert('{0}');window.location.replace('{1}')</script>", message, url);

			if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "AlertAndRedirect"))
				page.ClientScript.RegisterStartupScript(page.GetType(), "AlertAndRedirect", js);
		}

		/// <summary>
		/// 回到历史页面
		/// </summary>
		/// <param name="value">-1/1</param>
		public static void GoHistory(Page page, int value) {
			string js = string.Format(@"<script language='javascript'>history.go({0});</script>", value);

			if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "GoHistory"))
				page.ClientScript.RegisterStartupScript(page.GetType(), "GoHistory", js);
		}

		/// <summary>
		/// 刷新父窗口
		/// </summary>
		public static void RefreshParent(Page page, string url) {
			string js = string.Format(@"<script language='javascript'>window.opener.location.href='{0}';window.close();</script>", url);

			if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshParent"))
				page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshParent", js);
		}


		/// <summary>
		/// 刷新打开窗口
		/// </summary>
		public static void RefreshOpener(Page page) {
			string js = @"<script language='javascript'>opener.location.reload();</script>";

			if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshOpener"))
				page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshOpener", js);
		}


		/// <summary>
		/// 打开指定大小的新窗体
		/// </summary>
		/// <param name="url">地址</param>
		/// <param name="width">宽</param>
		/// <param name="heigth">高</param>
		/// <param name="top">头位置</param>
		/// <param name="left">左位置</param>
		public static void OpenWebFormSize(Page page, string url, int width, int heigth, int top, int left) {
			string js = string.Format(@"<script language='javascript'>window.open('{0}','','height={1},width={2},top={3},left={4},location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</script>",
				url, heigth, width, top, left);

			if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "OpenWebFormSize"))
				page.ClientScript.RegisterStartupScript(page.GetType(), "OpenWebFormSize", js);
		}


		/// <summary>
		/// 转向Url制定的页面
		/// </summary>
		/// <param name="url">连接地址</param>
		public static void JavaScriptLocationHref(Page page, string url) {
			string js = string.Format(@"<script language='javascript'>window.location.replace('{0}');</script>", url);

			if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "JavaScriptLocationHref"))
				page.ClientScript.RegisterStartupScript(page.GetType(), "JavaScriptLocationHref", js);
		}

		/// <summary>
		/// 打开指定大小位置的模式对话框
		/// </summary>
		/// <param name="webFormUrl">连接地址</param>
		/// <param name="width">宽</param>
		/// <param name="height">高</param>
		/// <param name="top">距离上位置</param>
		/// <param name="left">距离左位置</param>
		public static void ShowModalDialogWindow(Page page, string webFormUrl, int width, int height, int top, int left) {
			string features = string.Format(@"dialogWidth:{0}px;dialogHeight:{1}px;dialogLeft:{2}px;dialogTop:{3}px;center:yes;help=no;resizable:no;status:no;scroll=yes",
				width, height, left, top);

			ShowModalDialogWindow(page, webFormUrl, features);
		}

		/// <summary>
		/// 弹出模态窗口
		/// </summary>
		/// <param name="webFormUrl"></param>
		/// <param name="features"></param>
		public static void ShowModalDialogWindow(Page page, string webFormUrl, string features) {
			string js = string.Format(@"<script language='javascript'>showModalDialog('{0}','','{1}');</script>", webFormUrl, features);

			if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "ShowModalDialogWindow"))
				page.ClientScript.RegisterStartupScript(page.GetType(), "ShowModalDialogWindow", js);
		}
	}
}
